package com.xhsj.user.hr.system.service;

import com.alibaba.fastjson.JSONObject;
import com.xhsj.user.hr.system.entity.TbGlobalUser;
import com.xhsj.user.hr.system.entity.TbOperationRecords;
import com.xhsj.user.hr.system.mapper.TbGlobalUserMapper;
import com.xhsj.user.hr.system.mapper.TbOperationRecordsMapper;
import com.xhsj.user.hr.system.service.TbOperationRecordsService;
import com.xhsj.user.hr.base.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.security.IntegrationAuthentication;
import com.xhsj.user.security.IntegrationAuthenticationContext;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.UserUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 操作记录表 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since Apr 28, 2021 6:17:19 AM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbOperationRecords")
public class TbOperationRecordsService extends BaseService<TbOperationRecordsMapper, TbOperationRecords>{
    @Autowired
    private TbOperationRecordsMapper dao;

    @Autowired
    private TbGlobalUserMapper tbGlobalUserMapper;

	/**
	 * 保存
	 * @author suxiaolin
	 * @date Apr 28, 2021 6:17:19 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbOperationRecords", allEntries=true)
    public boolean save(TbOperationRecords entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author suxiaolin
	 * @date Apr 28, 2021 6:17:19 AM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbOperationRecords getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Apr 28, 2021 6:17:19 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbOperationRecords", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author suxiaolin
	 * @date Apr 28, 2021 6:17:19 AM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author suxiaolin
	 * @date Apr 28, 2021 6:17:19 AM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbOperationRecords> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbOperationRecords entity = JSON.parseObject(JSON.toJSONString(data), TbOperationRecords.class);
        Page<TbOperationRecords> page = new Page(currentPage,pageSize);
        QueryWrapper<TbOperationRecords> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author suxiaolin
	 * @date Apr 28, 2021 6:17:19 AM
	 */
    @Override
    //@Cacheable(value = "TbOperationRecords",sync = true)
    public List<TbOperationRecords> selectAll() {
        return super.selectAll();
    }


    /**
     * 保存登录日志
     * @author 苏小林
     * @date 2021/4/28 16:11
     */
    public void saveLoginRecords(Object o) {
        IntegrationAuthentication integrationAuthentication = IntegrationAuthenticationContext.get();
        String username = integrationAuthentication.getAuthParameter("username");
        int status = 0;
        if (o instanceof Exception) {
            status = 1;
        }

        TbOperationRecords records = new TbOperationRecords();
        TbGlobalUser byPhoneNumber = tbGlobalUserMapper.findByPhoneNumber(username);
        if (byPhoneNumber != null) {
            records.setCreateName(byPhoneNumber.getFullName());
            records.setCreateBy(byPhoneNumber.getUserId());
            records.setUpdateBy(byPhoneNumber.getUserId());
        } else {
            records.setCreateName("不存在用户");
            records.setCreateBy(1L);
            records.setUpdateBy(1L);
            records.setRemarks(username);
        }
        records.setModule("HR模块");
        records.setOperation("登录系统");
        records.setCreateDate(new Date());
        records.setUpdateDate(new Date());
        records.setStatus(status);
        dao.insert(records);

    }
}
